<?php
/**
 * 管理中心· 财务管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Controller;
use Think\Page;
use Think\AjaxPage;
class IncomeController extends BaseController {
    //初始化程序
    public function _initialize()
    {
        parent::_initialize();
        
        $timegap['beg'] = date('Y-m-d',strtotime("-1 month"));//1个月
        $timegap['end'] = date('Y-m-d',strtotime("+1 days"));
        $this->assign('timegap',$timegap);
    }
    /******************************* 商城会员资金相关  ********************************/
    /**
     +--------------------------------------------------------------------------
     * 实现获取商城会员资金明细记录
     * @author:  XC跃
     * @date :2018-06-12
     +--------------------------------------------------------------------------
     */
    public function acUserList(){
        $this->assign('usid',trim(I('get.id')));
        $this->display();
    }
    public function ajaxAcUserList(){
        $usid = trim(I('usid'));
        //实例化相关表
        $acident = M('account');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        I('usid') ? $condition['ac_uid'] = I('usid') : false;
        //dump($condition);
        $sort_order = I('order_by').' '.I('sort');
        //echo $sort_order;
        $count = $acident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $acident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        //echo $acident->getLastSql();
//        dump($list);
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('rows',$Page->totalRows);
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现调节 商城会员资金的功能
     * @author:  XC跃
     * @date :2018-06-12
     +--------------------------------------------------------------------------
     */
    public function upUserIncome(){
        $usid = trim(I('id'));
        //实例化相关表
        $usident = M('user');
        $usFind = $usident->where("`us_uid`='".$usid."'")->find();
        
        if(IS_POST){
            $desc = trim(I('post.desc'));
            //加减用户余额
            $type_money = trim(I('post.monType'));
            $user_money = trim(I('post.money/f'));
            if($user_money!='0'){
                if($type_money=='0' && $user_money>$usFind['us_money']){
                    $res = array('status'=>-1,'info'=>'用户剩余余额不足');
                    exit(json_encode($res));
                }
                $user_money = $type_money ? $user_money : 0-$user_money;
            }
            //加减用户冻结积分
            $type_point = trim(I('post.poinType'));
            $user_point = trim(I('post.points/f'));
            if($user_point!='0'){
                if($type_point=='0' && $user_point>$usFind['us_newpoints']){
                    $res = array('status'=>-1,'info'=>'用户剩余冻结积分不足');
                    exit(json_encode($res));
                }
                $user_point = $type_point ? $user_point : 0-$user_point;
            }
            //加减用户购物积分
            $type_frozen= trim(I('post.froType'));
            $user_frozen= trim(I('post.frozen/f'));
            if($user_frozen!='0'){
                if($type_frozen=='0' && $user_frozen>$usFind['us_frozen']){
                    $res = array('status'=>-1,'info'=>'用户剩余购物积分不足');
                    exit(json_encode($res));
                }
                $user_frozen = $type_frozen ? $user_frozen : 0-$user_frozen;
            }
            //加减用户股权
            $type_equity= trim(I('post.equType'));
            $user_equity= trim(I('post.equity/f'));
            if($user_equity!='0'){
                if($type_equity=='0' && $user_equity>$usFind['us_equity']){
                    $res = array('status'=>-1,'info'=>'用户剩余股权不足');
                    exit(json_encode($res));
                }
                $user_equity = $type_equity ? $user_equity : 0-$user_equity;
            }
//            if($user_frozen!='0'){
//                $frozen = $type_frozen ? $user_frozen : 0-$user_frozen;
//                //$frozen = $usFind['us_frozen']+$frozen;  //计算用户被冻结的资金
//                //增加冻结资金
//                if($type_frozen=='1' && $user_frozen>$usFind['us_money']){
//                    $res = array('status'=>-1,'info'=>'用户剩余资金不足');
//                    exit(json_encode($res));
//                }
//                //减少冻结资金
//                if($type_frozen=='0' && $user_frozen>$usFind['us_frozen']){
//                    $res = array('status'=>-1,'info'=>'用户冻结资金不足');
//                    exit(json_encode($res));
//                }
//                $user_money = $type_frozen ? 0-$user_frozen : $user_frozen ;    //计算用户剩余资金
//            }
            $acres = accShopLog($usid,$user_money,$user_point,$user_frozen,$user_equity,0,$desc);
            if($acres){
                $res = array('status'=>1,'info'=>'资金调节成功');
                exit(json_encode($res));
            }else{
                $res = array('status'=>-1,'info'=>'资金调节失败');
                exit(json_encode($res));
            }
        }else{
            $this->assign('re',$usFind);
            $this->display();
        }
    }
    /********************************* 提现记录相关  *************************************/
    /**
    +--------------------------------------------------------------------------
     * 实现获取提现记录的功能
     * @author:  XC跃
     * @date :2018-11-22
    +--------------------------------------------------------------------------
     */
    public function cashList(){
        $this->display();
    }
    public function ajaxCashList(){
      //实例化相关表
        $icident = M('cash');
        $baident = M('bank');
        // 搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
//        dump($condition);
        if($beg && $end){
            $condition['ic_atime'] = array('between',"$beg,$end");
        }
        $sort_order = I('order_by').' '.I('sort');
        $count = $icident->where($condition)->count();
        $Page  = new AjaxPage($count,50);
        $show = $Page->show();
        $list = $icident->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach($list as $k=>$v){
           $list[$k]['bank'] = $baident->where("`ba_id`='".$v['ic_bank']."'")->find();
        }
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出

        $this->assign('rows',$Page->totalRows);
        $this->display();
    }

    /**
    +--------------------------------------------------------------------------
     * 实现管理员对提现的具体执行功能
     * @author:  XC跃
     * @date :2018-11-22
    +--------------------------------------------------------------------------
     */
    public function cashConf(){
        //接收传值
        $cid = trim(I('post.id'));
        $type= trim(I('post.ty'));
        $titl= trim(I('post.ti'));
        //实例化相关表
        $caident = M('cash');
        $usident = M('user');
        $acident = M('account');
        //获取提现记录
        $icFind = $caident->where("`ic_id`='".$cid."'")->find();
        if(count($icFind)=='0'){
            $res= array('status'=>-1,'msg'=>'该记录不存在或无效记录'); // 返回结果状态
            exit(json_encode($res));
        }

        if('1'==$type){  //审核成功
            //更改提现记录状态
            $re = $caident->where("`ic_id`='".$cid."'")->setField('ic_state','1');
            //添加操作日志
            addLog(session('admin_auth.usname'),'2','2','审核ID'.$cid.'的提现记录');
        }else{
            //拒绝提现
            //退还会员资金
            $data['us_money'] = array('exp','us_money+'.$icFind['ic_money']);
            $usident->where("`us_uid`='".$icFind['ic_uid']."'")->save($data);
            //删除资金记录
            $acident->where("`ac_batch`='".$icFind['ic_batch']."'")->delete();
            //更改提现状态
            $cdata['ic_state'] = '2';
            $cdata['ic_remark']= $remark = trim(I('post.re'));
            $re = $caident->where("`ic_id`='".$cid."'")->save($cdata);
            //发送站内通知
//            $meidentr = new MessageModel();
//            $smsArray = array(
//              'title'=> '提现申请拒绝',
//              'body' => $remark,
//              'suid' => $icFind['ic_uid'],
//              'fuid' => session('admin_auth.usname')
//            );
//            $meidentr->sms_send($smsArray);
        }
        if($re){
            $res= array('status'=>1,'msg'=>'提现'.$titl.'成功'); // 返回结果状态
        }else{
            $res= array('status'=>-1,'msg'=>'提现'.$titl.'失败'); // 返回结果状态
        }exit(json_encode($res));
    }
    /**
    +--------------------------------------------------------------------------
     * 实现导出管理员提现记录的功能
     * @author:  XC跃
     * @date :2018-11-22
    +--------------------------------------------------------------------------
     */
    public function cashExport(){
        $xlsName  = "提现记录明细";
        $xlsCell  = array(
          array('ic_id','编号'),
          array('ic_batch','流水号'),
          array('ic_uid','会员编号'),
          array('ic_uname','真实姓名'),
          array('ic_phone','联系电话'),
          array('ic_bank','提现信息'),
          array('ic_money','提现金额'),
          array('ic_amount','到账金额'),
          array('ic_atime','申请时间'),
          array('ic_state','状态')
        );
        //实例化相关表
        $caident = M('cash');
        $baident = M('bank');
        $usident = M('user');
        //搜索条件
        $beg = strtotime(I('stime'));
        $end = strtotime(I('etime'));
        $condition = array();
        I('keyword') ? $condition[I('xcinput')] = array('LIKE',"%".I('keyword')."%") : false;
        if($beg && $end){
            $condition['ic_atime'] = array('between',"$beg,$end");
        }
        $xlsData = $caident->where($condition)->order("`ic_id` desc")->select();

        //echo $uident->getLastSql();
        foreach($xlsData as $k=>$v){
            $xlsData[$k]['ca_id']  = $k+1;
            $bank =  $baident->where("`ba_id`='".$v['ic_bank']."'")->find();
            $xlsData[$k]['ic_uname']= $bank['ba_uname'];
            $xlsData[$k]['ic_phone']= $bank['ba_phone'];
            $xlsData[$k]['ic_bank'] = $bank['ba_name'].":".$bank['ba_card'];

            $xlsData[$k]['ic_atime']= Date("Y-m-d H:i:s",$v['ic_atime']);
            if('1'==$v['ic_state']){
                $xlsData[$k]['ic_state'] = "已完成";
            }elseif('2'==$v['ic_state']){
                $xlsData[$k]['ic_state'] = "已拒绝";
            }elseif('3'==$v['ic_state']){
                $xlsData[$k]['ic_state'] = "进行中";
            }
        }
        exportExcel($xlsName,$xlsCell,$xlsData);
    }


}